﻿/////////////////////////////////////////////////////////////////////////////////
//
// InstantCube
// - Automatically generates Analysis Services cubes
// (C) Copyright Johan Åhlén 2009
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
// See the GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
//
// More info on InstantCube website: http://instantcube.codeplex.com
//
/////////////////////////////////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AdvantIQ.InstantCube.Model
{
    public class AttributePart
    {
        public AttributePartType PartType { get; set; }
        public string FieldName { get; set; }
        public string FieldType { get; set; }

        public enum AttributePartType
        {
            Key,
            Name,
            Value
        }

        public AttributePart(AttributePartType ID, string type, string attributeName, Project project)
        {
            Init(ID, type, attributeName, project);
        }

        public AttributePart(XElement srcNode, string attributeName, Project project)
        {
            if (srcNode == null)
                return;

            try
            {
                AttributePartType id;
                if (!AttributePartType.TryParse(srcNode.Name.ToString(), true, out id))
                {
                    throw new ModelErrorException(this, "AttributePart: Invalid part type");
                }
                var type = srcNode.Attribute("type").SafeValue();
                Init(id, type, attributeName, project);
            }
            catch (ModelErrorException ex)
            {
                throw new ModelErrorException(this, ex);
            }
            catch (Exception ex)
            {
                throw new ModelErrorException(this, ex.Message);
            }
        }

        public void Init(AttributePartType ID, string type, string attributeName, Project project)
        {
            this.PartType = ID;
            FieldType = !string.IsNullOrEmpty(type) ? type : GetDefaultType(project, ID);
            FieldName = attributeName + GetSuffix(project, ID);
        }

        private string GetDefaultType(Project project, AttributePartType partId)
        {
            switch (partId)
            {
                case AttributePartType.Key:
                    return project.DefaultTypes.Key;
                case AttributePartType.Name:
                    return project.DefaultTypes.Name;
                case AttributePartType.Value:
                    return project.DefaultTypes.Value;
                default:
                    throw new Exception();
            }
        }

        private string GetSuffix(Project project, AttributePartType partId)
        {
            switch (partId)
            {
                case AttributePartType.Key:
                    return project.Namings.KeySuffix;
                case AttributePartType.Name:
                    return project.Namings.NameSuffix;
                case AttributePartType.Value:
                    return project.Namings.ValueSuffix;
                default:
                    throw new Exception();
            }
        }

        private string Normalize(string str)
        {
            return str.Substring(0, 1).ToUpper() + str.Substring(1).ToLower();
        }

        public override string ToString()
        {
            return string.Format("AttributePart({0})", PartType);
        }
    }
}
